import 'package:flutter/material.dart';
import 'package:hiot_cloud_app_v1/app/core/values/app_colors.dart';

enum ButtonVariant {
  filled,
  outlined,
  text,
}

class CustomButton extends StatelessWidget {
  final String text;
  final VoidCallback? onPressed;
  final ButtonVariant variant;
  final double? width;
  final double? height;
  final Color? color;
  final EdgeInsets? padding;
  final Widget? icon;
  final bool loading;

  const CustomButton({
    Key? key,
    required this.text,
    this.onPressed,
    this.variant = ButtonVariant.filled,
    this.width,
    this.height,
    this.color,
    this.padding,
    this.icon,
    this.loading = false,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final Color buttonColor = color ?? AppColors.primary;
    
    // 处理禁用状态
    final bool isDisabled = onPressed == null;
    
    switch (variant) {
      case ButtonVariant.filled:
        return _buildFilledButton(buttonColor, isDisabled);
      case ButtonVariant.outlined:
        return _buildOutlinedButton(buttonColor, isDisabled);
      case ButtonVariant.text:
        return _buildTextButton(buttonColor, isDisabled);
    }
  }
  
  Widget _buildFilledButton(Color color, bool isDisabled) {
    return SizedBox(
      width: width,
      height: height ?? 48,
      child: ElevatedButton(
        onPressed: isDisabled || loading ? null : onPressed,
        style: ElevatedButton.styleFrom(
          backgroundColor: isDisabled ? Colors.grey.shade300 : color,
          foregroundColor: Colors.white,
          padding: padding ?? const EdgeInsets.symmetric(horizontal: 16),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(12),
          ),
          elevation: 0,
        ),
        child: _buildButtonContent(
          color: Colors.white,
          isDisabled: isDisabled,
        ),
      ),
    );
  }
  
  Widget _buildOutlinedButton(Color color, bool isDisabled) {
    return SizedBox(
      width: width,
      height: height ?? 48,
      child: OutlinedButton(
        onPressed: isDisabled || loading ? null : onPressed,
        style: OutlinedButton.styleFrom(
          foregroundColor: isDisabled ? Colors.grey : color,
          padding: padding ?? const EdgeInsets.symmetric(horizontal: 16),
          side: BorderSide(
            color: isDisabled ? Colors.grey.shade300 : color,
            width: 1.5,
          ),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(12),
          ),
        ),
        child: _buildButtonContent(
          color: isDisabled ? Colors.grey : color,
          isDisabled: isDisabled,
        ),
      ),
    );
  }
  
  Widget _buildTextButton(Color color, bool isDisabled) {
    return SizedBox(
      width: width,
      height: height ?? 40,
      child: TextButton(
        onPressed: isDisabled || loading ? null : onPressed,
        style: TextButton.styleFrom(
          foregroundColor: isDisabled ? Colors.grey : color,
          padding: padding ?? const EdgeInsets.symmetric(horizontal: 8),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(8),
          ),
        ),
        child: _buildButtonContent(
          color: isDisabled ? Colors.grey : color,
          isDisabled: isDisabled,
        ),
      ),
    );
  }
  
  Widget _buildButtonContent({
    required Color color,
    required bool isDisabled,
  }) {
    if (loading) {
      return SizedBox(
        width: 24,
        height: 24,
        child: CircularProgressIndicator(
          strokeWidth: 2,
          valueColor: AlwaysStoppedAnimation<Color>(
            variant == ButtonVariant.filled ? Colors.white : color,
          ),
        ),
      );
    }
    
    if (icon != null) {
      return Row(
        mainAxisSize: MainAxisSize.min,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          icon!,
          const SizedBox(width: 8),
          Text(
            text,
            style: TextStyle(
              fontWeight: FontWeight.w500,
              fontSize: 16,
            ),
          ),
        ],
      );
    }
    
    return Text(
      text,
      style: TextStyle(
        fontWeight: FontWeight.w500,
        fontSize: 16,
      ),
    );
  }
} 